package timeseries
import (
"fmt"
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/links"
"github.com/K-Phoen/grabana/scheme"
"github.com/K-Phoen/grabana/timeseries/axis"
"github.com/K-Phoen/grabana/timeseries/fields"
"github.com/K-Phoen/grabana/timeseries/threshold"
"github.com/K-Phoen/sdk"
)
type Option func (timeseries *TimeSeries ) error
type TooltipMode string
const (
SingleSeries TooltipMode = "single"
AllSeries TooltipMode = "multi"
NoSeries TooltipMode = "none"
)
type StackMode string
const (
Unstacked StackMode = "none"
NormalStack StackMode = "normal"
PercentStack StackMode = "percent"
)
type LineInterpolationMode string
const (
Linear LineInterpolationMode = "linear"
Smooth LineInterpolationMode = "smooth"
StepBefore LineInterpolationMode = "stepBefore"
StepAfter LineInterpolationMode = "stepAfter"
)
type BarAlignment int
const (
AlignCenter BarAlignment = 0
AlignBefore BarAlignment = -1
AlignAfter BarAlignment = 1
)
type GradientType string
const (
NoGradient GradientType = "none"
Opacity GradientType = "opacity"
Hue GradientType = "hue"
Scheme GradientType = "scheme"
)
type LegendOption uint16
const (
Hide LegendOption = iota
AsTable
AsList
Bottom
ToTheRight
Min
Max
Avg
First
FirstNonNull
Last
LastNonNull
Total
Count
Range
)
type TimeSeries struct {
Builder *sdk .Panel
Alert *alert .Alert
}
func New (title string , options ...Option ) (*TimeSeries , error ) {
panel := &TimeSeries {Builder : sdk .NewTimeseries (title )}
panel .Builder .IsNew = false
for _ , opt := range append (defaults (), options ...) {
if err := opt (panel ); err != nil {
return nil , err
}
}
return panel , nil
}
func defaults() []Option {
return []Option {
Span (6 ),
LineWidth (1 ),
FillOpacity (25 ),
PointSize (5 ),
Tooltip (SingleSeries ),
Legend (Bottom , AsList ),
Lines (Linear ),
GradientMode (Opacity ),
Axis (
axis .Placement (axis .Auto ),
axis .Scale (axis .Linear ),
),
}
}
func Links (panelLinks ...links .Link ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Links = make ([]sdk .Link , 0 , len (panelLinks ))
for _ , link := range panelLinks {
timeseries .Builder .Links = append (timeseries .Builder .Links , link .Builder )
}
return nil
}
}
func DataSource (source string ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Datasource = &sdk .DatasourceRef {LegacyName : source }
return nil
}
}
func Tooltip (mode TooltipMode ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .Options .Tooltip .Mode = string (mode )
return nil
}
}
func LineWidth (value int ) Option {
return func (timeseries *TimeSeries ) error {
if value < 0 || value > 10 {
return fmt .Errorf ("line width must be between 0 and 10: %w" , errors .ErrInvalidArgument )
}
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .LineWidth = value
return nil
}
}
func Stack (value StackMode ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .Stacking .Mode = string (value )
return nil
}
}
func FillOpacity (value int ) Option {
return func (timeseries *TimeSeries ) error {
if value < 0 || value > 100 {
return fmt .Errorf ("fill opacity must be between 0 and 100: %w" , errors .ErrInvalidArgument )
}
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .FillOpacity = value
return nil
}
}
func PointSize (value int ) Option {
return func (timeseries *TimeSeries ) error {
if value < 0 || value > 40 {
return fmt .Errorf ("point size must be between 0 and 40: %w" , errors .ErrInvalidArgument )
}
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .PointSize = value
return nil
}
}
func Lines (mode LineInterpolationMode ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .LineInterpolation = string (mode )
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .DrawStyle = "line"
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .LineStyle = struct {
Fill string `json:"fill"`
}{
Fill : "solid" ,
}
return nil
}
}
func Bars (alignment BarAlignment ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .BarAlignment = int (alignment )
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .DrawStyle = "bars"
return nil
}
}
func Points () Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .DrawStyle = "points"
return nil
}
}
func GradientMode (mode GradientType ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .TimeseriesPanel .FieldConfig .Defaults .Custom .GradientMode = string (mode )
return nil
}
}
func Axis (options ...axis .Option ) Option {
return func (timeseries *TimeSeries ) error {
_ , err := axis .New (×eries .Builder .TimeseriesPanel .FieldConfig , options ...)
return err
}
}
func Thresholds (options ...threshold .Option ) Option {
return func (timeseries *TimeSeries ) error {
threshold .New (×eries .Builder .TimeseriesPanel .FieldConfig , options ...)
return nil
}
}
func ColorScheme (options ...scheme .Option ) Option {
return func (timeseries *TimeSeries ) error {
scheme .New (×eries .Builder .TimeseriesPanel .FieldConfig , options ...)
return nil
}
}
func Legend (opts ...LegendOption ) Option {
return func (timeseries *TimeSeries ) error {
yup := true
legend := sdk .TimeseriesLegendOptions {
Show : &yup ,
DisplayMode : "list" ,
Placement : "bottom" ,
Calcs : make ([]string , 0 ),
}
for _ , opt := range opts {
switch opt {
case Hide :
nope := false
legend .DisplayMode = "hidden"
legend .Show = &nope
case AsList :
legend .DisplayMode = "list"
case AsTable :
legend .DisplayMode = "table"
case ToTheRight :
legend .Placement = "right"
case Bottom :
legend .Placement = "bottom"
case First :
legend .Calcs = append (legend .Calcs , "first" )
case FirstNonNull :
legend .Calcs = append (legend .Calcs , "firstNotNull" )
case Last :
legend .Calcs = append (legend .Calcs , "last" )
case LastNonNull :
legend .Calcs = append (legend .Calcs , "lastNotNull" )
case Min :
legend .Calcs = append (legend .Calcs , "min" )
case Max :
legend .Calcs = append (legend .Calcs , "max" )
case Avg :
legend .Calcs = append (legend .Calcs , "mean" )
case Count :
legend .Calcs = append (legend .Calcs , "count" )
case Total :
legend .Calcs = append (legend .Calcs , "sum" )
case Range :
legend .Calcs = append (legend .Calcs , "range" )
default :
return fmt .Errorf ("unknown legend option: %w" , errors .ErrInvalidArgument )
}
}
timeseries .Builder .TimeseriesPanel .Options .Legend = legend
return nil
}
}
func Span (span float32 ) Option {
return func (timeseries *TimeSeries ) error {
if span < 1 || span > 12 {
return fmt .Errorf ("span must be between 1 and 12: %w" , errors .ErrInvalidArgument )
}
timeseries .Builder .Span = span
return nil
}
}
func Height (height string ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Height = &height
return nil
}
}
func Description (content string ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Description = &content
return nil
}
}
func Transparent () Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Transparent = true
return nil
}
}
func Alert (name string , opts ...alert .Option ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Alert = alert .New (timeseries .Builder .Title , append (opts , alert .Summary (name ))...)
timeseries .Alert .Builder .Name = timeseries .Builder .Title
return nil
}
}
func Repeat (repeat string ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .Repeat = &repeat
return nil
}
}
func RepeatDirection (direction sdk .RepeatDirection ) Option {
return func (timeseries *TimeSeries ) error {
timeseries .Builder .RepeatDirection = &direction
return nil
}
}
func FieldOverride (m fields .Matcher , opts ...fields .OverrideOption ) Option {
return func (timeseries *TimeSeries ) error {
override := sdk .FieldConfigOverride {}
m (&override )
for _ , opt := range opts {
opt (&override )
}
timeseries .Builder .TimeseriesPanel .FieldConfig .Overrides = append (timeseries .Builder .TimeseriesPanel .FieldConfig .Overrides , override )
return nil
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .